package util

import java.util.regex.Pattern

import org.apache.commons.codec.digest.DigestUtils

class Utils {

	static Pattern patId = ~/\d+/
	static Pattern patOpenid = ~/([a-z]|[A-Z]|\_|\-|\d){10,50}/

	static boolean isIdValid(String str){
		str ==~ patId
	}

	static boolean isOpenidValid(String str){
		str ==~ patOpenid
	}

	static String encode(String str){
		DigestUtils.shaHex(str)
	}

	static String uuid(){
		'' + System.currentTimeMillis() + '_' + new Random().nextInt(10000)
	}

	static List getRecentWeekList(int num){
		def dateRangeList = []
		def dd = new Date()
		def begin = dd - dd.day

		// 查询最近10周的，选择项
		num.times{
			def date = begin - it * 7
			dateRangeList << date.format('yyyy-MM-dd')
		}

		dateRangeList
	}
}